package com.jin.spirng_boot_learn.controllers;

import com.jin.spirng_boot_learn.stk.bean.ResponseStatus;
import com.jin.spirng_boot_learn.stk.bean.ResponseStatusList;
import com.jin.spirng_boot_learn.stk.bean.ResponseStatusObject;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

@RestController
@RequestMapping("/test")
public class TestController {

    @RequestMapping(value = "/stk-datacenter/rest", method = {RequestMethod.PUT, RequestMethod.POST})
    public @ResponseBody Object createStudents(@RequestBody String info, HttpServletRequest request) {
        Enumeration headerNames = request.getHeaderNames();
        while (headerNames.hasMoreElements()) {
            String key = (String) headerNames.nextElement();
            String value = request.getHeader(key);
            System.out.println(key+value);
        }
        System.out.println(info);
        return getForkResult();
    }
    private ResponseStatusList getForkResult() {
        ResponseStatusList responseStatusList = new ResponseStatusList();
        ResponseStatusObject responseStatusObject = new ResponseStatusObject();
        List<ResponseStatus> list = new ArrayList<>();
        list.add(new ResponseStatus());
        list.add(new ResponseStatus());
        responseStatusObject.setResponseStatusList(list);
        responseStatusList.setResponseStatusObject(responseStatusObject);
        return responseStatusList;
    }


    @RequestMapping(value = "/head", method = {RequestMethod.HEAD})
    public @ResponseBody Object reserveHead() {
        return "hello world";
    }


    @RequestMapping(value = "/post-put", method = {RequestMethod.POST, RequestMethod.PUT})
    public @ResponseBody Object postPutServer(@RequestBody String request,
                                               @RequestHeader(HttpHeaders.AUTHORIZATION) String accessToken) {
        System.out.println(request);
        System.out.println(accessToken);
        return "{\"hello\":\"world\"}";
    }


    @RequestMapping(value = "/delete", method = {RequestMethod.DELETE})
    public @ResponseBody Object deleteServer(@RequestParam String code,
                                               @RequestHeader(HttpHeaders.AUTHORIZATION) String accessToken) {
        System.out.println(code);
        System.out.println(accessToken);
        return "{\"hello\":\"world\"}";
    }


}
